package org.jeecg.modules.demo.st.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.st.entity.StProfession;
import org.jeecg.modules.demo.st.entity.StStudent;
import org.jeecg.modules.demo.st.mapper.StProfessionMapper;
import org.jeecg.modules.demo.st.mapper.StStudentMapper;
import org.jeecg.modules.demo.st.service.IStProfessionService;
import org.jeecg.modules.demo.st.vo.*;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.impl.SysDepartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 专业
 * @Author: jeecg-boot
 * @Date:   2019-10-26
 * @Version: V1.0
 */
@Service
public class StProfessionServiceImpl extends ServiceImpl<StProfessionMapper, StProfession> implements IStProfessionService {

    @Autowired
    private StProfessionMapper stProfessionMapper;
    @Autowired
    private StStudentMapper stStudentMapper;
    @Autowired
    private ISysDepartService sysDepartService;

    @Override
    public Map<String,Object> getListsPicDataBySchool(){
        //获取当前用户
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
        List<String> prodepartIds=new ArrayList<>();
        for (SysDepart sysDepart : departs) {
            prodepartIds.add(sysDepart.getId());
        }
        String departIds = "'"+StringUtils.join(prodepartIds,"','")+"'";
        Map<String,Object> picData=new HashMap<>();
        QueryWrapper<StStudent> stStudentQueryWrapper=new QueryWrapper<>();
        stStudentQueryWrapper.inSql("college","select id from sys_depart where id in ("+departIds+") or parent_id in (" +departIds+"  ) and del_flag=0" );
        stStudentQueryWrapper.eq("deleted","0");
        stStudentQueryWrapper.eq("field_one","0");
        Integer selectCount = stStudentMapper.selectCount(stStudentQueryWrapper);
        picData.put("persons",selectCount);
        List<Map<String,Object>> statusAll = stProfessionMapper.getStatusAll(departIds);
        for (int i = 0; i < statusAll.size(); i++) {
            picData.put(statusAll.get(i).get("status")+"",statusAll.get(i).get("cou"));
        }
        return picData;
    }
    @Override
    public Map<String,Object> getListsPicDataByDepart(String departIds){
        Map<String,Object> picData=new HashMap<>();
        QueryWrapper<StStudent> stStudentQueryWrapper=new QueryWrapper<>();
        stStudentQueryWrapper.eq("college",departIds );
        stStudentQueryWrapper.eq("deleted","0");
        stStudentQueryWrapper.eq("field_one","0");
        Integer selectCount = stStudentMapper.selectCount(stStudentQueryWrapper);
        picData.put("persons",selectCount);
        List<Map<String,Object>> statusAll = stProfessionMapper.getStatusAllByDepart(departIds);
        for (int i = 0; i < statusAll.size(); i++) {
            picData.put(statusAll.get(i).get("status")+"",statusAll.get(i).get("cou"));
        }
        return picData;
    }
    @Override
    public Map<String,Object> getListsPicDataByProfession(String professionIds){
        Map<String,Object> picData=new HashMap<>();
        QueryWrapper<StStudent> stStudentQueryWrapper=new QueryWrapper<>();
        stStudentQueryWrapper.eq("profession",professionIds);
        stStudentQueryWrapper.eq("deleted","0");
        stStudentQueryWrapper.eq("field_one","0");
        Integer selectCount = stStudentMapper.selectCount(stStudentQueryWrapper);
        picData.put("persons",selectCount);
        List<Map<String,Object>> statusAll = stProfessionMapper.getStatusAllByProfession(professionIds);
        for (int i = 0; i < statusAll.size(); i++) {
            picData.put(statusAll.get(i).get("status")+"",statusAll.get(i).get("cou"));
        }
        return picData;
    }
    @Override
    public Map<String,Object> getListsPicDataByClass(String classIds){
        Map<String,Object> picData=new HashMap<>();
        QueryWrapper<StStudent> stStudentQueryWrapper=new QueryWrapper<>();
        stStudentQueryWrapper.eq("st_class",classIds);
        stStudentQueryWrapper.eq("deleted","0");
        stStudentQueryWrapper.eq("field_one","0");
        Integer selectCount = stStudentMapper.selectCount(stStudentQueryWrapper);
        picData.put("persons",selectCount);
        List<Map<String,Object>> statusAll = stProfessionMapper.getStatusAllByClass(classIds);
        for (int i = 0; i < statusAll.size(); i++) {
            picData.put(statusAll.get(i).get("status")+"",statusAll.get(i).get("cou"));
        }
        return picData;
    }

    @Override
    public Map<String, Object> getListsGrade(String fieldTwos,String  departIds) {
        Map<String,Object> picData=new HashMap<>();
        QueryWrapper<StStudent> stStudentQueryWrapper=new QueryWrapper<>();
        stStudentQueryWrapper.eq("field_two",fieldTwos);
        stStudentQueryWrapper.eq("deleted","0");
        stStudentQueryWrapper.eq("field_one","0");
        if(StringUtils.isNotBlank(departIds)){
            stStudentQueryWrapper.eq("college",departIds);
        }
        Integer selectCount = stStudentMapper.selectCount(stStudentQueryWrapper);
        picData.put("persons",selectCount);
        List<Map<String,Object>> statusAll = stProfessionMapper.getListsGrade(fieldTwos,departIds);
        for (int i = 0; i < statusAll.size(); i++) {
            picData.put(statusAll.get(i).get("status")+"",statusAll.get(i).get("cou"));
        }
        return picData;
    }


    /**
     * 专业
     * 通过学院获取专业接口
     * getStProfession
     */
    @Override
    public List<ProfessionListVo> getStProfession(String id) {
        List<ProfessionListVo>st= stProfessionMapper.getStProfession(id);
        return st;
    }


    /**
     * 专业
     * 通过学校id获取学院列表
     * getLists
     */
    @Override
    public List<DepartListVo> getLists() {
        //获取当前用户
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
        List<String> departIds=new ArrayList<>();
        for (SysDepart sysDepart : departs) {
            departIds.add(sysDepart.getId());
        }
        String departIds1 = "'"+StringUtils.join(departIds,"','")+"'";
        List<DepartListVo> tt = stProfessionMapper.getLists(departIds1);
        return tt;
    }

    @Override
    public List<GradeListsVo> getGradeLists(String departIds) {
        List<GradeListsVo> tt = stProfessionMapper.getGradeLists(departIds);
        return tt;
    }

    @Override
    public List<NationPersonsVo> getGradeListsSt(String departIds, String professionIds, String classIds, String fieldTwo) {
        List<NationPersonsVo> nationPeopleVos= stProfessionMapper.getGradeListsSt(departIds,fieldTwo,professionIds,classIds);
        return nationPeopleVos;
    }
}
